package com.imu.purse.modules.sys.controller;

import java.util.List;
import java.util.Map;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.github.pagehelper.PageInfo;
import com.imu.common.annotation.SysLog;
import com.imu.common.utils.R;
import com.imu.common.validator.ValidatorUtils;
import com.imu.common.validator.group.AddGroup;
import com.imu.common.validator.group.UpdateGroup;
import com.imu.purse.modules.sys.entity.SysCarouselEntity;
import com.imu.purse.modules.sys.service.SysCarouselService;
import com.xiaoleilu.hutool.util.BeanUtil;

/**
 * 轮播图配置表
 * @author sunhongwei
 * @email sunhongwei@jshijian.com
 * @date 2017-11-06 17:17:24
 * Copyright (c) 2017 武汉极时间科技有限公司 版权所有 
 * Jshijian aloha CO.,LTD. All Rights Reserved. 
 */
@RestController
@RequestMapping("/sys/carousel")
@SuppressWarnings("deprecation")
public class SysCarouselController extends com.imu.purse.modules.sys.controller.AbstractController
{
	/****
	 * 查询轮播图配置表
	 * @param params
 	 * @date 2017-11-06 17:17:24
	 * @return
	 */
	@SysLog("查询轮播图配置表")
	@GetMapping("/list")
	@RequiresPermissions("sys:carousel:list")
	public R list(@RequestParam Map<String, Object> params)
	{
		R r = R.ok();
		if(CollectionUtils.isEmpty(params)) return r;
		
		SysCarouselEntity dto = BeanUtil.mapToBean(params, SysCarouselEntity.class, true);
		List<SysCarouselEntity> dtos = service.queryList(dto);
		if(CollectionUtils.isEmpty(dtos)) return r;
		
		return r.put("page", new PageInfo<SysCarouselEntity>(dtos));
	}
	
	/****
	 * 根据ID查询轮播图配置表
	 * @param carId
 	 * @date 2017-11-06 17:17:24
	 * @return
	 */
	@SysLog("根据ID查询轮播图配置表")
	@GetMapping("/info/{carId}")
	@RequiresPermissions("sys:carousel:info")
	public R info(@PathVariable("carId") Long carId)
	{
		return R.ok().put("dto", service.findById(carId));
	}
	
	/****
	 * 新增轮播图配置表
	 * @param dto
 	 * @date 2017-11-06 17:17:24
	 * @return
	 */
	@SysLog("新增轮播图配置表")
	@PostMapping("/save")
	@RequiresPermissions("sys:carousel:save")
	public R save(@RequestBody SysCarouselEntity dto)
	{
		ValidatorUtils.validateEntity(dto, AddGroup.class);
		dto.setUserCreate(this.getUserId());
		dto.setUserUpdate(this.getUserId());
		service.save(dto);
		return R.ok();
	}
	
	/****
	 * 修改轮播图配置表
	 * @param dto
 	 * @date 2017-11-06 17:17:24
	 * @return
	 */
	@SysLog("修改轮播图配置表")
	@PostMapping("/update")
	@RequiresPermissions("sys:carousel:update")
	public R update(@RequestBody SysCarouselEntity dto)
	{
		ValidatorUtils.validateEntity(dto, UpdateGroup.class);
		dto.setUserUpdate(this.getUserId());
		service.update(dto);
		return R.ok();
	}
	
	/****
	 * 删除轮播图配置表
	 * @param ids
 	 * @date 2017-11-06 17:17:24
	 * @return
	 */
	@SysLog("删除轮播图配置表")
	@PutMapping("/delete")
	@RequiresPermissions("sys:carousel:delete")
	public R delete(@RequestBody String[] ids)
	{
		service.delete(ids);
		return R.ok();
	}
	
	@Autowired
	private SysCarouselService service;
}
